package org.jeecg.modules.prodentry.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.manage.entity.CustomerInfo;
import org.jeecg.modules.manage.service.ICustomerInfoService;
import org.jeecg.modules.prodentry.entity.ProdEntry;
import org.jeecg.modules.prodentry.service.IProdEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

 /**
 * @Description:  成品仓出入库
 * @Author: jerryzhu
 * @Date:   2021-11-15
 * @Version: V1.0
 */
@Api(tags=" 成品仓出入库")
@RestController
@RequestMapping("/prodEntry")
@Slf4j
public class ProdEntryController extends JeecgController<ProdEntry, IProdEntryService> {
	
	@Autowired
	private IProdEntryService prodEntryService;
	
	@Autowired
	private ICustomerInfoService customerInfoService;
	
	
	
	/**
	 *成品仓出入库明细分页列表
	 *
	 * @param prodEntry
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-明细分页")
	@ApiOperation(value="成品仓出入库-明细分页列表", notes="成品仓出入库-明细分页列表")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ProdEntry prodEntry,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ProdEntry> queryWrapper = QueryGenerator.initQueryWrapper(prodEntry, req.getParameterMap());
		Page<ProdEntry> page = new Page<ProdEntry>(pageNo, pageSize);
		IPage<ProdEntry> pageList = prodEntryService.selectProdEntryPage(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 仓库库存数据列表
	 *
	 * @param prodEntry
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-仓库库存数据列表")
	@ApiOperation(value="成品仓出入库-仓库库存数据列表", notes="成品仓出入库-仓库库存数据列表")
	@GetMapping(value = "/queryStorageData")
	public Result<?> queryStorageData(@RequestParam(name="storageId",required = true) String storageId)  {
		List<ProdEntry> list = prodEntryService.getProdEntryStorageSummaryList(storageId);
		return Result.OK(list);
	}
	
	/**
	 * 员工库存数据列表
	 *
	 * @param goldEntry
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-员工库存数据列表")
	@ApiOperation(value="成品仓出入库-员工库存数据列表", notes="成品仓出入库-员工库存数据列表")
	@GetMapping(value = "/queryPersonData")
	public Result<?> queryPersonData(@RequestParam(name="userId",required = true) String userId)  {
		List<ProdEntry> list = prodEntryService.getProdEntryPersonSummaryList(userId);
		return Result.OK(list);
	}
	
	/**
	 *   批量添加
	 *
	 * @param prodEntry
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-批量添加")
	@ApiOperation(value="成品仓出入库-批量添加", notes="成品仓出入库-批量添加")
	@PostMapping(value = "/batchAdd")
	@Transactional
	public Result<?> batchAdd(@RequestBody JSONObject jsonObject) {
		String sendPerson = jsonObject.getString("sendPerson");//发出员工
		String sendStorage = jsonObject.getString("sendStorage");//发出仓库
		String sendCustomer = jsonObject.getString("sendCustomer");//发出客户
		String receivePerson = jsonObject.getString("receivePerson");//接收员工
		String receiveStorage = jsonObject.getString("receiveStorage");//接收仓库
		String receiveCustomer = jsonObject.getString("receiveCustomer");//接收客户
		Integer type = jsonObject.getInteger("type");//出入库类型
		String jsonString = JSONArray.toJSONString(jsonObject.get("data"));
		JSONArray jsonArray = JSONArray.parseArray(jsonString);
		Date now = new Date();//当前时间
		if(jsonArray != null && jsonArray.size() > 0) {
			String sn = "PON-"+System.currentTimeMillis();//操作流水号
			for(int i = 0; i<jsonArray.size(); i++) {
				String str = JSONArray.toJSONString(jsonArray.get(i));
				JSONObject obj = JSONObject.parseObject(str);
				String id = obj.getString("id");//主键ID
				if(id != null && !"".equals(id)) {
					ProdEntry prodEntry = new ProdEntry();
					prodEntry.setProdActionType(type);//操作类型
					prodEntry.setSn(sn);//操作流水号
					prodEntry.setSendPerson(sendPerson);//发出员工
					prodEntry.setSendStorage(sendStorage);//发出仓库
					prodEntry.setSendCustomer(sendCustomer);//发出客户
					prodEntry.setSendTime(now);//发出时间
					prodEntry.setReceivePerson(receivePerson);//接收员工
					prodEntry.setReceiveStorage(receiveStorage);//接收仓库
					prodEntry.setReceiveCustomer(receiveCustomer);//接收客户
					prodEntry.setReceiveTime(now);//接收时间
					prodEntry.setWorkNum(obj.getString("workNum"));//工单号
					prodEntry.setPn(obj.getString("pn"));//拆分页
					prodEntry.setOrderInfoId(obj.getString("orderInfoId"));//订单号
					prodEntry.setStyleCusInfoId(obj.getString("styleCusInfoId"));//款号
					prodEntry.setKarat(obj.getString("karat"));//成色
					prodEntry.setCount(obj.getInteger("count"));//数量
					prodEntry.setGoldWeight(obj.getBigDecimal("goldWeight"));//净金重
					prodEntry.setPureStoneWeight(obj.getBigDecimal("pureStoneWeight"));//石重
					prodEntry.setWithStoneWeight(obj.getBigDecimal("withStoneWeight"));//连石重
					prodEntry.setUnitPrice(obj.getBigDecimal("unitPrice"));//单价金额(￥)
					prodEntry.setCostAmount(obj.getBigDecimal("costAmount"));//价值金额(￥)
					prodEntry.setPayAmount(obj.getBigDecimal("payAmount"));//实付金额(￥)
					prodEntry.setRemark(obj.getString("remark"));//备注
					prodEntryService.save(prodEntry);
				}
			}
			return Result.ok("操作成功！");
		} else {
			return Result.error("操作失败，请勾选要保存的数据！");
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param prodEntry
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-编辑")
	@ApiOperation(value="成品仓出入库-编辑", notes="成品仓出入库-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ProdEntry prodEntry) {
		prodEntryService.updateById(prodEntry);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-通过id删除")
	@ApiOperation(value="成品仓出入库-通过id删除", notes="成品仓出入库-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required = true) String id) {
		prodEntryService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-批量删除")
	@ApiOperation(value="成品仓出入库-批量删除", notes="成品仓出入库-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required = true) String ids) {
		this.prodEntryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-通过id查询")
	@ApiOperation(value="成品仓出入库-通过id查询", notes="成品仓出入库-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required = true) String id) {
		ProdEntry prodEntry = prodEntryService.getById(id);
		if(prodEntry==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(prodEntry);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param prodEntry
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ProdEntry prodEntry) {
        return super.exportXls(request, prodEntry, ProdEntry.class, "成品仓出入库");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ProdEntry.class);
    }
    
	/**
	 *  获取发出/接收客户的姓名
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-获取接收客户的姓名")
	@ApiOperation(value="成品仓出入库-获取接收客户的姓名", notes="成品仓出入库-获取接收客户的姓名")
	@GetMapping(value = "/getCustomer")
	public Result<?> getCustomer(@RequestParam(name="id",required = true) String id) {
		CustomerInfo customerInfo = customerInfoService.getById(id);
		return Result.OK(customerInfo.getCusName());
	}
	
	/**
	 *  获取发出/接收仓库的姓名
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "成品仓出入库-获取接收仓库的姓名")
	@ApiOperation(value="成品仓出入库-获取接收仓库的姓名", notes="成品仓出入库-获取接收仓库的姓名")
	@GetMapping(value = "/getStorage")
	public Result<?> getStorage(@RequestParam(name="id",required = true) String id) {
		String sendStorage = prodEntryService.getStorageName(id);
		return Result.OK(sendStorage);
	}
}
